using System.Collections;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

public class ScrollLevels : MonoBehaviour
{
	public ScrollRectController scrollController;

	public RectTransform[] layers;

	private CanvasGroup[] layersCanvasGroup;

	public Arena[] arenas;

	public Image[] imagesAniLevel;

	public Image[] imagesAniLevelPoints;

	public bool isAnimating;

	private bool firstUpdate = true;

	[HideInInspector]
	public float alpha = 1f;

	private Tweener tweenAlpha;

	private Tween tweenLevelBg;

	private Tweener[] tweenLevelPoints = new Tweener[5];

	private void Update()
	{
		if (firstUpdate)
		{
			firstUpdate = false;
			Init();
			SetParenting();
			UpdateAspect();
		}
	}

	public void Init()
	{
		layersCanvasGroup = new CanvasGroup[layers.Length];
		for (int i = 0; i < layers.Length; i++)
		{
			layersCanvasGroup[i] = layers[i].gameObject.GetComponent<CanvasGroup>();
		}
		float num = 0f;
		float num2 = num;
		RectTransform content = scrollController.scrollRect.content;
		int levelsCount = Globals.levelsCount;
		int num3 = levelsCount / 6;
		if (levelsCount % 6 > 0)
		{
			num3++;
		}
		arenas = new Arena[num3 + 2];
		arenas[0] = null;
		ArenaLevel original = Resources.Load<ArenaLevel>("ArenaLevels");
		for (int j = 1; j <= num3; j++)
		{
			ArenaLevel arenaLevel = Object.Instantiate(original) as ArenaLevel;
			arenaLevel.arenaNumber = j;
			arenaLevel.scrollLevels = this;
			arenas[j] = arenaLevel;
			arenaLevel.rectTransform.SetParent(content);
			arenaLevel.rectTransform.localScale = Vector3.one;
			arenaLevel.rectTransform.localPosition = new Vector3(num2, -80f, 0f);
			arenaLevel.rectTransform.anchoredPosition = new Vector3(num2, -80f, 0f);
			arenaLevel.Init();
			num2 += arenaLevel.rectTransform.sizeDelta.x;
		}
		ArenaSoon original2 = Resources.Load<ArenaSoon>("ArenaSoon");
		ArenaSoon arenaSoon = Object.Instantiate(original2) as ArenaSoon;
		num3++;
		int num4 = (arenaSoon.arenaNumber = num3);
		arenaSoon.scrollLevels = this;
		arenas[num4] = arenaSoon;
		arenaSoon.rectTransform.SetParent(content);
		arenaSoon.rectTransform.localScale = Vector3.one;
		arenaSoon.rectTransform.localPosition = new Vector3(num2, -80f, 0f);
		arenaSoon.rectTransform.anchoredPosition = new Vector3(num2, -80f, 0f);
		arenaSoon.Init();
		num2 += arenaSoon.rectTransform.sizeDelta.x;
		num2 += num;
		content.sizeDelta = new Vector2(num2, content.sizeDelta.y);
		content.localScale *= RectTransformAspectScaler.GetScaleForAspectRatio(1f);
		RectTransform[] array = layers;
		foreach (RectTransform rectTransform in array)
		{
			rectTransform.sizeDelta = content.sizeDelta;
			rectTransform.localPosition = content.localPosition;
			rectTransform.localScale *= RectTransformAspectScaler.GetScaleForAspectRatio(1f);
		}
	}

	public void SetParenting()
	{
		Arena[] array = arenas;
		foreach (Arena arena in array)
		{
			if (arena != null)
			{
				arena.SetParenting();
			}
		}
		Image[] array2 = imagesAniLevelPoints;
		foreach (Image image in array2)
		{
			image.rectTransform.SetAsLastSibling();
		}
		Image[] array3 = imagesAniLevel;
		foreach (Image image2 in array3)
		{
			image2.rectTransform.SetAsLastSibling();
		}
	}

	public void UpdateAspect()
	{
		Arena[] array = arenas;
		foreach (Arena arena in array)
		{
			if (arena != null)
			{
				arena.UpdateAspect();
			}
		}
	}

	public RectTransform GetLayer(int layerIdx)
	{
		if (layerIdx < layers.Length)
		{
			return layers[layerIdx];
		}
		Debug.Log("WTF ScrollLevels : canvas layer not found - idx=" + layerIdx);
		return null;
	}

	public void SetAlpha(float targetAlpha)
	{
		alpha = targetAlpha;
		for (int i = 0; i < layersCanvasGroup.Length; i++)
		{
			layersCanvasGroup[i].alpha = targetAlpha;
		}
		scrollController.gameObject.SetActive(targetAlpha != 0f);
	}

	public void FadeTo(float targetAlpha, float duration)
	{
		if (tweenAlpha != null)
		{
			tweenAlpha.Kill();
			tweenAlpha = null;
		}
		tweenAlpha = DOTween.To(SetAlpha, alpha, targetAlpha, duration);
	}

	public void FadeIn(float duration = 0.25f)
	{
		FadeTo(1f, duration);
	}

	public void FadeOut(float duration = 0.25f)
	{
		FadeTo(0f, duration);
	}

	public void StartHighlightNextLevel()
	{
		StopHighlightNextLevel();
		int highlightArenaNumber;
		int highlightLevelNumber;
		GetHighlightLevel(out highlightArenaNumber, out highlightLevelNumber);
		if (highlightArenaNumber != -1 && highlightLevelNumber != -1)
		{
			int num = highlightArenaNumber;
			int num2 = highlightLevelNumber - 1 - (highlightArenaNumber - 1) * 6;
			RectTransform rectTransform = arenas[num].levelsTitle[num2].rectTransform;
			UserInterface.GetInstance().StartHighlighting(rectTransform, 1.8f, 1f);
		}
	}

	public void StopHighlightNextLevel()
	{
		UserInterface.GetInstance().StopHighlighting(1f);
	}

	public void GetHighlightLevel(out int highlightArenaNumber, out int highlightLevelNumber)
	{
		highlightArenaNumber = -1;
		highlightLevelNumber = -1;
		int num = -1;
		int num2 = -1;
		for (int i = 1; i <= Globals.levelsCount; i++)
		{
			int arenaNumber = GetArenaNumber(i);
			if (EconomySettings.GetUnlockPointsForArena(arenaNumber) <= Economy.totalPoints || Economy.arenasPurchased)
			{
				Economy.LevelData levelData = Economy.GetLevelData(i);
				if (levelData.record == 0f && num == -1)
				{
					num = i;
				}
				if (levelData.record > 0f)
				{
					num2 = i;
				}
			}
		}
		if (num2 != -1)
		{
			int num3 = num2 + 1;
			int num4 = (num3 - 1) / 6 + 1;
			if (num3 <= Globals.levelsCount && (EconomySettings.GetUnlockPointsForArena(num4) <= Economy.totalPoints || Economy.arenasPurchased))
			{
				highlightArenaNumber = num4;
				highlightLevelNumber = num3;
				return;
			}
		}
		if (num != -1)
		{
			int num5 = (num - 1) / 6 + 1;
			if (EconomySettings.GetUnlockPointsForArena(num5) <= Economy.totalPoints || Economy.arenasPurchased)
			{
				highlightArenaNumber = num5;
				highlightLevelNumber = num;
			}
		}
	}

	public void ShowLevelPoints(int levelNumber, int fromPoints, int toPoints)
	{
		isAnimating = true;
		Vector2[] array = new Vector2[5]
		{
			new Vector2(-72f, 33f),
			new Vector2(0f, 33f),
			new Vector2(72f, 33f),
			new Vector2(-36f, -32f),
			new Vector2(36f, -32f)
		};
		int arenaNumber = GetArenaNumber(levelNumber);
		ArenaLevel arena = arenas[arenaNumber] as ArenaLevel;
		Image points = arena.levelsPoints[levelNumber - 1 - (arenaNumber - 1) * 6];
		Text label = arena.levelsTitle[levelNumber - 1 - (arenaNumber - 1) * 6];
		label.rectTransform.anchoredPosition = arena.levelsTitleInitialPositions[levelNumber - 1 - (arenaNumber - 1) * 6];
		points.gameObject.SetActive(true);
		points.sprite = SpriteManager.GetInstance().spritePoints[fromPoints];
		int num = 0;
		for (int i = fromPoints; i < toPoints; i++)
		{
			bool isLastPoint = i == toPoints - 1;
			Image targetAniImage = imagesAniLevelPoints[i];
			Sprite targetEndSprite = SpriteManager.GetInstance().spritePoints[i + 1];
			targetAniImage.rectTransform.SetParent(points.rectTransform);
			targetAniImage.rectTransform.anchoredPosition = array[i];
			targetAniImage.rectTransform.localScale = Vector3.one * 1.5f;
			targetAniImage.gameObject.SetActive(true);
			targetAniImage.color = new Color(1f, 1f, 1f, 0f);
			int arenaCurrentPoints = arena.currentPoints + i - fromPoints + 1;
			tweenLevelPoints[i] = targetAniImage.rectTransform.DOScale(Vector3.one, 0.3f).SetEase(Ease.OutCubic).SetDelay((float)num * 0.2f)
				.OnStart(delegate
				{
					label.color = Color.white;
					Audio.PlaySoundPoint();
					targetAniImage.color = new Color(1f, 1f, 1f, 1f);
					arena.centerUnlockedLabel.text = arenaCurrentPoints.ToString();
				})
				.OnComplete(delegate
				{
					points.sprite = targetEndSprite;
					targetAniImage.gameObject.SetActive(false);
					if (isLastPoint)
					{
						if (toPoints == 5)
						{
							OnShowLastPointComplete(levelNumber);
						}
						else
						{
							OnShowLevelPointsComplete(levelNumber);
						}
					}
				});
			num++;
		}
	}

	private void OnShowLastPointComplete(int levelNumber)
	{
		int arenaNumber = GetArenaNumber(levelNumber);
		ArenaLevel arenaLevel = arenas[arenaNumber] as ArenaLevel;
		Image bg = arenaLevel.levelsBg[levelNumber - 1 - (arenaNumber - 1) * 6];
		Image image = arenaLevel.levelsPoints[levelNumber - 1 - (arenaNumber - 1) * 6];
		Image wildcard = arenaLevel.levelsWildcards[levelNumber - 1 - (arenaNumber - 1) * 6];
		wildcard.transform.localScale = Vector3.zero;
		wildcard.gameObject.SetActive(true);
		image.rectTransform.DOScale(Vector3.zero, 0.3f).SetEase(Ease.InCubic).SetDelay(0f)
			.OnComplete(delegate
			{
				bg.sprite = SpriteManager.GetInstance().spriteLevelFreePlay;
				bg.rectTransform.localScale = Vector3.one * 1.15f;
				bg.rectTransform.DOScale(Vector3.one, 0.2f).SetEase(Ease.OutCubic);
				wildcard.rectTransform.localScale = Vector3.one * 1.15f;
				wildcard.rectTransform.DOScale(Vector3.one, 0.2f).SetEase(Ease.OutCubic).OnComplete(delegate
				{
					OnShowLevelPointsComplete(levelNumber);
				});
			});
	}

	private void OnShowLevelPointsComplete(int levelNumber)
	{
		int arenaNumber = GetArenaNumber(levelNumber);
		ArenaLevel arenaLevel = arenas[arenaNumber] as ArenaLevel;
		arenaLevel.UpdateAspect();
		isAnimating = false;
	}

	private int GetArenaNumber(int levelNumber)
	{
		return (levelNumber - 1) / 6 + 1;
	}

	public void ShowUnlockArena(int arenaNumber, bool doScroll = true, bool doSound = true)
	{
		StartCoroutine(ShowUnlockArenaCo(arenaNumber, doScroll, doSound));
	}

	private IEnumerator ShowUnlockArenaCo(int arenaNumber, bool doScroll, bool doSound)
	{
		isAnimating = true;
		ArenaLevel arena = arenas[arenaNumber] as ArenaLevel;
		if (doScroll)
		{
			scrollController.ScrollHorizontalLerpToChild(arena.rectTransform, true);
			yield return null;
			yield return null;
			while (scrollController.isScrolling)
			{
				yield return null;
			}
		}
		yield return null;
		arena.centerBg.sprite = SpriteManager.GetInstance().spriteArenaUnlocked;
		arena.centerUnlockedLabel.text = "0";
		arena.centerLockedLock.gameObject.SetActive(false);
		arena.centerLockedPoint.gameObject.SetActive(false);
		arena.centerLockedLabel.gameObject.SetActive(false);
		arena.centerUnlockedPoint.gameObject.SetActive(true);
		arena.centerUnlockedLabel.gameObject.SetActive(true);
		yield return new WaitForSeconds(0.05f);
		for (int i = 0; i < arena.levelsBg.Length; i++)
		{
			if (doSound)
			{
				Audio.PlaySoundUnlockLevel();
			}
			arena.levelsTitle[i].color = new Color(0.9254902f, 0.8509804f, 0.7490196f, 1f);
			arena.levelsPoints[i].sprite = SpriteManager.GetInstance().spritePoints[0];
			arena.levelsBg[i].sprite = SpriteManager.GetInstance().spriteLevelUnplayable;
			arena.levelsBg[i].rectTransform.localScale = Vector3.one * 1.15f;
			arena.levelsBg[i].rectTransform.DOScale(Vector3.one, 0.2f).SetEase(Ease.OutCubic);
			yield return new WaitForSeconds(0.1f);
		}
		isAnimating = false;
	}
}
